<?php
declare (strict_types=1);

namespace app\exam\model\admin;

use app\admin\logic\user\Account;
use app\common\model\BaseModel;
use app\common\logic\category\Category;

/**
 * 后台管理
 */
class QuestionBank extends BaseModel
{

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_question_bank';
    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';
    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';
    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'               => 'integer',
        'status'           => 'integer',
        // 'open'             => 'integer',
        'fullcategorypath' => 'json',
    ];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 机构
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 关联创建用户
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/31
     *
     * @return \think\model\relation\BelongsTo
     */
    public function createUser()
    {
        return $this->belongsTo(Account::class, 'admin_user_id')->removeOption('soft_delete')->bind(['user_name']);
    }

    /**
     * 分类数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-07
     *
     * @param    [type]                         $category [description]
     * @param    [type]                         $data     [description]
     */
    public function setCategoryAttr($category): ?string
    {
        if (is_string($category) || is_null($category) || !is_array($category)) return $category;
        return $category ? ',' . implode(',', array_unique($category)) . ',' : '';
    }

    /**
     * 获取分类全路径名称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-17
     * @param  [type] $v    [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function getCategoryStrAttr($v, $data)
    {
        return $data['category'] ? app(Category::class)->parseTitle($data['category'], 3) : '';
    }

    /**
     * 获取状态选项
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/23
     *
     * @return array[]
     */
    public function getStatusOptions()
    {
        return [
            ['title' => '不限', 'value' => -1],
            ['title' => '启用', 'value' => 1],
            ['title' => '禁用', 'value' => 0],
        ];
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     *
     * @return   string
     */
    public static function onBeforeInsert($bank)
    {
        // 机构
        $bank->mhm_id        = $bank->getAttr('mhm_id') ?: request()->mhm_id;
        $bank->admin_user_id = $bank->getAttr('admin_user_id') ?: request()->admin_user_id;
        $bank->status        = 1;
    }

    /**
     * 成功创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterInsert($category)
    {
    }

    /**
     * 成功修改之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterUpdate($category)
    {
    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterDelete($category)
    {
    }

}
